package com.zhi.syc.data_applist.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.zip.GZIPOutputStream;

public class ASUtil {

    public static String stringToGZIP(String sourceString){
        String zipString = "";

        if(TextUtils.isEmpty(sourceString)){
            return zipString;
        }

        try {
            String mUtf8Char = "UTF-8";
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
            gzipOutputStream.write(sourceString.getBytes(mUtf8Char));
            gzipOutputStream.close();

            byte[] zipBuffer = outputStream.toByteArray();
            zipString = Base64.encodeToString(zipBuffer, Base64.URL_SAFE);
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return zipString;
    }

    public static String safeString(String sourceString){
        if(TextUtils.isEmpty(sourceString)){
            sourceString = "";
        }
        return sourceString;
    }

    public static String getFileName(String filePath) {
        try {
            int start = filePath.lastIndexOf("/");
            if (start != -1) {
                return filePath.substring(start + 1);
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @SuppressLint("SimpleDateFormat")
    public static long getStartTime() {
        try {
            Calendar mCalendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int year = mCalendar.get(Calendar.YEAR);
            int month = mCalendar.get(Calendar.MONTH) + 1;
            int day = mCalendar.get(Calendar.DAY_OF_MONTH);

            Date date = format.parse(year + "-" + month + "-" + day + " 00:00:00");
            if (date != null) {
                return date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0L;
    }

    @SuppressLint("SimpleDateFormat")
    public static long getEndTime() {
        try {
            Calendar mCalendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int year = mCalendar.get(Calendar.YEAR);
            int month = mCalendar.get(Calendar.MONTH) + 1;
            int day = mCalendar.get(Calendar.DAY_OF_MONTH);

            Date date = format.parse(year + "-" + month + "-" + day + " 23:59:59");
            if (date != null) {
                return date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0L;
    }


    @SuppressLint("SimpleDateFormat")
    public static long getStartTimeForNewRecord() {
        try {
            Calendar mCalendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int year = mCalendar.get(Calendar.YEAR);
            int month = mCalendar.get(Calendar.MONTH) + 1;
            int day = mCalendar.get(Calendar.DAY_OF_MONTH);

            Date date = format.parse(year + "-" + month + "-" + day + " 23:59:59");
            if (date != null) {
                return date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0L;
    }

    @SuppressLint("SimpleDateFormat")
    public static long getEndTimeForNextDays(int nextDays) {
        try {
            Calendar mCalendar = new GregorianCalendar();
            mCalendar.setTime(new Date());
            //把日期往后增加一天.整数往后推,负数往前移动
            mCalendar.add(Calendar.DATE, nextDays);

            Date startDate = mCalendar.getTime();
            return startDate.getTime();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }
}
